<?php
App::uses('AppController', 'Controller');

class LibretasController extends AppController {
     public $uses = array('Gestione', 'Paralelo', 'MateriasParalelo', 'Ciclo', 'AlumnosParciale', 'AlumnosParalelo');
    public $layout = 'colegio';
    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('*');
    }
    
    public function index(){
        if(!empty($this->request->data)){
          
            $paralelo = $this->request->data['Libretas']['paralelo'];
            $gestion = $this->request->data['Libretas']['gestion'];
            $ciclo = $this->request->data['Libretas']['ciclo'];
            
            $alumnos = $this->AlumnosParalelo->find('all', array('recursive' => 0,
                'conditions' => array('AlumnosParalelo.paralelo_id' => $paralelo), 
                'order'=>array('Alumno.ap_paterno ASC', 'Alumno.ap_materno ASC', 'Alumno.nombre ASC'),
                'recursive'=>0));
         
            $c = 0;
            $arrayAlumnos = array();
    
            
            foreach ($alumnos as $a) {
                $arrayAlumnos[$c] = $a['AlumnosParalelo']['alumno_id'];
                $c++;
            }
           
            $notas = $this->AlumnosParciale->find('all', array(
               'conditions'=>array(
               'AlumnosParciale.alumno_id'=>$arrayAlumnos,
                'AlumnosParciale.gestion'=>$gestion,
                'AlumnosParciale.ciclo_id'=>$ciclo,
                'AlumnosParciale.promedio !='=>0 
               ), 
               'order'=>array('Materia.orden ASC', 'Alumno.ap_paterno ASC', 'Alumno.ap_materno ASC', 'Alumno.nombre ASC')
           ));
           
           $this->set(compact('notas', 'alumnos', 'gestion'));
        }
        
           $gestiones = $this->Gestione->find('all', array('fields'=>array('Gestione.nombre')));
           $ciclos= $this->Ciclo->find('list', array('fields'=>array('Ciclo.id', 'Ciclo.nombre')));
           $this->set(compact('gestiones', 'ciclos'));
        
    }
    public function ajaxciclosgestion($gestion=null){
        $this->layout = 'ajax';
        $ciclos= $this->Ciclo->find('all', 
        array(
            'fields'=>array('Ciclo.id', 'Ciclo.nombre'),
            'conditions'=>array('Ciclo.gestion'=>$gestion)
            )
        );
        //debug($ciclos);
        $this->set(compact('ciclos'));
    }
    public function ajaxparalelosgestion($gestion=null){
        $this->layout = 'ajax';
        $paralelos = $this->Paralelo->find('all', array(
            'conditions'=>array('Paralelo.gestion'=>$gestion), 
            'recursive'=>-1
        ));
        //debug($paralelos);
        $this->set(compact('paralelos'));
    }

}
?>